#!/user/bin/env python
# -*- coding: utf-8 -*-
''' 
Author: Sjy
LastEditTime: 2024-12-17 11:24:26
Description: 菜单和dock 命令集合
'''

__all__= []

import gui.controls.envs as envs

from .uis import ImportMainModel, ImportSubmodel
from .params_model import (DlgParamsModeling,DlgLoadBoundary,DlgMeshPartition,DlgPostProcessing, DlgCompositePly,
DlgAdhesiveLayer, DlgHoneycombCore, DlgMaterialSetting, DlgOptimization)
from .actions import Actions

class Pops:
    def import_main_model() -> None:
        """导入模型
        Args:
            None
        Returns:
            None
        """
        ImportMainModel()

    def import_submodel() -> None:
        """导入计算模型
        """
        ImportSubmodel()

    def show_dlg(flag) -> None:
        """显示对话框
        Args:
            flag : 对话框标识
        Returns:
            None
        """
        py3d= envs.GL.get_value("py3d")
        if flag == "paramsModeling":   
            dlgParamsModeling = DlgParamsModeling.show(py3d)
            if dlgParamsModeling:
                dlgParamsModeling.paramsModeling_save_signal.connect(lambda data: Pops.paramsModeling_save_signal(data))
        elif flag == "materialsProperties":  
            pass 
        elif flag == "meshPartition":
            dlgMeshPartition = DlgMeshPartition.show(py3d)
            if dlgMeshPartition:
                dlgMeshPartition.meshPartition_save_signal.connect(lambda data: Pops.meshPartition_save_signal(data))
        elif flag == "loadBoundary":   
            dlgLoadBoundary = DlgLoadBoundary.show(py3d)
            if dlgLoadBoundary:
                dlgLoadBoundary.loadBoundary_save_signal.connect(lambda data: Pops.loadBoundary_save_signal(data))
        elif flag == "optimizationMethod":   
            dlgOptimization = DlgOptimization.show(py3d)
            if dlgOptimization:
                dlgOptimization.optimization_save_signal.connect(lambda data: Pops.optimization_save_signal(data))
        elif flag == "postProcessing":   
            dlgPostProcessing = DlgPostProcessing.show(py3d)
            if dlgPostProcessing:
                dlgPostProcessing.postProcessing_save_signal.connect(lambda data: Pops.postProcessing_save_signal(data))
        
        elif flag == "materialSetting":  
            dlgMaterialSetting = DlgMaterialSetting.show(py3d)
            if dlgMaterialSetting:
                dlgMaterialSetting.materialSetting_save_signal.connect(lambda data: Pops.materialSetting_save_signal(data))         
        # if flag =="materiasingle": # 单个修改接口
        #     dlgMaterialsingle = DlgMaterialsingle.show(parent=py3d,materialType="蜂窝芯子",materialName="R1")
        #     if dlgMaterialsingle:
        #         dlgMaterialsingle.setwindowTitle('')
        #         dlgMaterialsingle.materialsingle_save_signal.connect(lambda data: Pops.materialsingle_save_signal(data))

    def paramsModeling_save_signal(data) -> None:
        """保存参数建模数据
        Args:
            data: 参数建模数据
        Returns:
            None
        """
        print("paramsModeling_save_signal=", data)
        delKeys = []
        if data.get("损伤类型") != "贯穿式损伤":
            delKeys = ["额外挖除尺寸", "下铺层修补胶层层数", "下铺层修补层层数", "下铺层修补层"]
        if not (data.get("损伤类型") == "蜂窝芯子损伤" or data.get("损伤类型") == "贯穿式损伤"):
            delKeys.append("蜂窝芯子修补层")
        Actions.save_data(data, delKeys=delKeys)
    def meshPartition_save_signal(data:dict={}) -> None:
        # print("meshPartition_save_signal", data)
        Actions.save_data(data)
    def loadBoundary_save_signal(data:dict={}) -> None:
        # print("loadBoundary_save_signal", data)
        Actions.save_data(data)
    def postProcessing_save_signal(data:dict={}) -> None:
        # print("postProcessing_save_signal", data)
        Actions.save_data(data)
    def optimization_save_signal(data:dict={}) -> None:
        print("optimization_save_signal=", data)
        
        Actions.save_data(data)
    def materialSetting_save_signal(data:dict={}) -> None:
        """材料设置
        {
        '上铺层': # 上铺层
            [{'material': {'E/Enn': '1.0', 'G1/Enn': '2.0', 'G2/Enn': '3.0', '名称': 'jc1'}, 'Angle': 2.0}, {'material': {'E/Enn': '2.0', 'G1/Enn': '3.0', 'G2/Enn': '4.0', '名称': 'jc2'}, 'Angle': 2.0}], 
        '下铺层': # 下铺层
            [{'material': {'Es': '4.0', 'Gs': '5.0', '名称': 'FW2', '壁厚t': '1.0', '密度ρ': '3.0', '边长l': '2.0'}, 'Angle': 1.0}, {'material': {'Es': '4.0', 'Gs': '5.0', '名称': 'FW1', '壁厚t': '1.0', '密度ρ': '34.0', '边长l': '2.0'}, 'Angle': 2.0}], 
        '上铺层修补层': # 上铺层修补层
            [{'material': {'E1': '1.0', 'E2': '2.0', 'G12': '4.0', 'G13': '5.0', 'G23': '6.0', 'Nu12': '3.0', '名称': 'test1'}, 'Angle': 2.0}, {'material': {'Es': '4.0', 'Gs': '5.0', '名称': 'FW1', '壁厚t': '1.0', '密度ρ': '34.0', '边长l': '2.0'}, 'Angle': 3.0}, {'material': {'E/Enn': '1.0', 'G1/Enn': '2.0', 'G2/Enn': '3.0', '名称': 'jc1'}, 'Angle': 4.0}], 
        '下铺层修补层':  # 下铺层修补层
            [{'material': {'E1': '1.0', 'E2': '2.0', 'G12': '4.0', 'G13': '5.0', 'G23': '6.0', 'Nu12': '3.0', '名称': 'test1'}, 'Angle': 3.0}, {'material': {'Es': '4.0', 'Gs': '5.0', '名称': 'FW1', '壁厚t': '1.0', '密度ρ': '34.0', '边长l': '2.0'}, 'Angle': 4.0}, {'material': {'E/Enn': '1.0', 'G1/Enn': '2.0', 'G2/Enn': '3.0', '名称': 'jc1'}, 'Angle': 5.0}], 
        '下铺层填充层': 下铺层 填充层
            [{'material': {'E1': '1.0', 'E2': '2.0', 'G12': '4.0', 'G13': '5.0', 'G23': '6.0', 'Nu12': '3.0', '名称': 'test1'}, 'Angle': 2.0}, {'material': {'E1': '5.0', 'E2': '5.0', 'G12': '2.0', 'G13': '3.0', 'G23': '4.0', 'Nu12': '1.0', '名称': 'test3'}, 'Angle': 3.0}], 
        '蜂窝芯子材料': # 蜂窝芯子材料
            {'Es': '4.0', 'Gs': '5.0', '名称': 'FW1', '壁厚t': '1.0', '密度ρ': '34.0', '边长l': '2.0'}
        '蜂窝芯子修补层': # 蜂窝芯子修补层
            {'E/Enn': '1.0', 'G1/Enn': '2.0', 'G2/Enn': '3.0', '名称': 'jc1'}
        }

        Args: 
            data: 材料设置数据
        Returns:
            None
        Description:
            1. 先获取参数化建模数据
            2. 将材料设置中的数据补全到对应参数化建模数据中的对应位置
        """
        # print("materialSetting_save_signal=", data)
        inputData = Actions.get_data() or {}
        # 批量更新 层数据
        for key in ["上铺层", "上铺层修补层", "下铺层", "下铺层修补层"]:
            inputData[key]  = Pops._update_materials(data, inputData, key)
        
        key = "蜂窝芯子材料"
        honeycombCoreInput = inputData.get(key, {})
        Pops.remove_item_keys(honeycombCoreInput)
        honeycombCoreInput.update(data.get(key, {}))
        inputData[key] = honeycombCoreInput

        # 更新 蜂窝芯子修补层 注意 蜂窝芯子损伤/贯穿式损伤时启用
        curType = inputData.get("损伤类型")
        key = "蜂窝芯子修补层"  # 目前只有一层
        if curType == "贯穿式损伤" or curType == "蜂窝芯子损伤":
            # print("===========================================================")
            honeycombCoreRepairInput = inputData.get(key, []) # 列表只有一层
            if len(honeycombCoreRepairInput) == 0:
                honeycombCoreRepairInput.append({})
            Pops.remove_item_keys(honeycombCoreRepairInput[0])
            honeycombCoreRepairInput[0].update(data.get(key, {}))
            inputData[key] = honeycombCoreRepairInput
            # print("honeycombCoreRepairInput=", honeycombCoreRepairInput)
        elif key in inputData:
            inputData.pop(key)
        key = "下铺层填充层"
        inputData[key] = [{**item.get("材料"), "铺层角度":item.get("铺层角度")} for item in data.get(key, [])]
        Actions.save_data(inputData) or {}

    def _update_materials(data, inputData, key):
        savedItems = inputData.get(key, [])
        curItems = data.get(key, []) 
        # 上铺层、下铺层、上铺层修补层、下铺层修补层 这些的层数必须是一一对应的关系
        if len(savedItems) == len(curItems):
            for index, item in enumerate(savedItems) :
                Pops.remove_item_keys(item)
                curItem = curItems[index]
                item.update(curItem.get("材料"))
                item.update({"铺层角度": curItem.get("铺层角度")})
            return savedItems
        else:
            print(f"{key} : 数据不匹配")
    def remove_item_keys(item:dict={}):
        materialType = item.get("材料类型")
        if materialType:
            # 如果材料类型存在 则检查属于哪种类型 需要删除原有的然后更新现在的
            keys = Pops.get_materil_keys(materialType)
            # 根据列表keys 中的key 批量移除 item中的数据
            for key in keys:
                item.pop(key)
    def get_materil_keys(materialType):
        # 取key 即可
        if materialType == "蜂窝芯子":
            return {'Es': '4.0', 'Gs': '5.0', '材料名称': 'FW1', '壁厚t': '1.0', '密度ρ': '34.0', '边长l': '2.0'}.keys()
        elif materialType == "胶层":
            return {'E/Enn': '1.0', 'G1/Enn': '2.0', 'G2/Enn': '3.0', '材料名称': 'jc1'}.keys()
        elif materialType == "复合材料单层板":
            return  {'E1': '1.0', 'E2': '2.0', 'G12': '4.0', 'G13': '5.0', 'G23': '6.0', 'Nu12': '3.0', '材料名称': 'test1'}.keys()
        
    
    